package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.dto.ClueTrackRecordDTO;
import com.ktjy.mapper.TbClueMapper;
import com.ktjy.mapper.TbClueTrackRecordMapper;
import com.ktjy.pojo.TbClueTrackRecord;
import com.ktjy.service.ITbClueTrackRecordService;
import com.ktjy.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 线索跟进记录 服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-08-19
 */
@Service
public class TbClueTrackRecordServiceImpl extends ServiceImpl<TbClueTrackRecordMapper, TbClueTrackRecord> implements ITbClueTrackRecordService {
    @Autowired
    TbClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    TbClueMapper clueMapper;

    @Override
    public IPage<TbClueTrackRecord> findPage(ClueTrackRecordDTO clueTrackRecordDTO, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page page=new Page(pageNo,pageSize);
        //构造查询条件
        QueryWrapper qw=new QueryWrapper();
        if(clueTrackRecordDTO.getClueId()!=null){
            qw.eq("clue_id",clueTrackRecordDTO.getClueId());
        }
        return clueTrackRecordMapper.selectPage(page,qw);
    }

    @Override
    @Transactional
    public void saveTrackRecord(TbClueTrackRecord clueTrackRecord) {
        //1.保存线索跟踪记录
        clueTrackRecordMapper.insert(clueTrackRecord);

        //2.修改线索状态为跟进中
        UpdateWrapper uw=new UpdateWrapper();
        uw.eq("id",clueTrackRecord.getClueId());
        uw.set("status",Constants.FOLLOWING);
        clueMapper.update(uw);
    }
}
